<?php
/**
 * @title 2466. 统计构造好字符串的方案数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $low
     * @param Integer $high
     * @param Integer $zero
     * @param Integer $one
     * @return Integer
     */
    function countGoodStrings($low, $high, $zero, $one) {
        /**
         * @since 2024-01-25 避免重复遍历
         */
        $this->dp = [];

        /**
         * @return
         */
        return $this->StringCount(0, $low, $high, $zero, $one);
    }

    /**
     * @title 长度为sLength的字符串，后续接0和1，有多少个好字符串
     * @author start2004
     * @since 2024-01-25 Created
     *
     * @param Integer $sLength
     * @param Integer $low
     * @param Integer $high
     * @param Integer $zero
     * @param Integer $one
     * @return Integer
     */
    function StringCount($sLength, $low, $high, $zero, $one){
        /**
         * @since 2024-01-25 命中缓存
         */
        $key = $sLength;
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2024-01-25 字符串数量
         */
        $stringCount = 0;

        /**
         * @since 2024-01-25 字符串s是一个好字符串
         */
        if($sLength >= $low AND $sLength <= $high){
            $stringCount++;
        } else {}

        /**
         * @since 2024-01-25 字符串s尾部接更多0和1
         */
        if($sLength+$zero <= $high){
            $stringCount += $this->StringCount($sLength+$zero, $low, $high, $zero, $one);
        } else {}
        if($sLength+$one <= $high){
            $stringCount += $this->StringCount($sLength+$one, $low, $high, $zero, $one);
        } else {}

        /**
         * @return
         */
        return $this->dp[$key] = $stringCount % (pow(10, 9)+7);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.25_6.php
 */
$datas = [
    [200,200,10,1], //
    [3,3,1,1], // 8
    [2,3,1,2], // 5
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->countGoodStrings(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
